package com.eagle.tool;

import com.eagle.annotation.Length;
import com.eagle.enitity.LengthEntity;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Optional;

/**
 * @author qinlinsen
 */
public class ReduceTest3 {
    @Test
    public void testCalculateLengthSum() {
        final Field[] fields = LengthEntity.class.getDeclaredFields();
        final Optional<Integer> lengthSumOptional = Arrays.stream(fields)
                //过滤
                .filter(field -> field.isAnnotationPresent(Length.class))
                //映射
                .map(field -> field.getAnnotation(Length.class).value())
                //汇总
                .reduce((x, y) -> x + y);

        final Integer sum = lengthSumOptional.get();
        System.out.println(sum);

        System.out.println(10 + 23 + 38 + 14 + 51 + 6);

        //用reduce计算的结果是对的
        Assert.assertEquals(Optional.of(10 + 23 + 38 + 14 + 51 + 6).get(), sum);

    }
}
